# -*- coding:utf-8 -*-
"""
describe：读取yaml文件
Author：winni
Email：mwn666888@163.com
Time: 2024/3/15
Software: PyCharm
"""

import yaml, os
# from common.tools import get_path


class ReadYaml(object):

    def __init__(self, filename, part=None):
        self.filename = filename
        self.part = part

    def read(self):
        with open(file=self.filename, mode="r", encoding='utf-8', ) as f:
            data = f.read()
        if self.part is None:
            data_yaml = yaml.load(data, Loader=yaml.FullLoader)
        else:
            data_yaml = yaml.load(data, Loader=yaml.FullLoader)[self.part]
        # keys=data_yaml.keys()
        # print(data_yaml)
        return data_yaml

    def write(self, content):
        with open(file=self.filename, mode='w', encoding='utf-8') as f:
            yaml.dump(content, f, Dumper=yaml.Dumper)



if __name__ == '__main__':
    from common.tools import get_path
    # result=ReadYaml(os.path.join(Config.test_datas_dir, "接口地址.yaml"),part="用户前端").read()
    # result=ReadYaml(os.path.join(Config.test_datas_dir, "es连接账号.yaml"),
    #                          part="sit_es").read()
    # result=ReadYaml(os.path.join(Config.test_datas_dir, "账号.yaml")).read()
    # result = ReadYaml(get_path('/TestDatas/商品管理/Test商品管理.yaml'),part="商品查询").read()
    # result = ReadYaml(get_path( "/data/接口地址.yaml"),part="项目集").read()[0]["查询项目集列表接口"]
    result = ReadYaml(get_path('config/http.yml')).read()['jiandanyun']
    # result = ReadYaml(get_path("/config/env_uat.yaml")).read()["host"]
    # result = ReadYaml(get_path("/config/env_uat.yaml")).read()["user"]["管理员winni"]
    # result = ReadYaml(get_path("/data/账号.yaml"), part="playwright").read()[0]
    print(result)
    # all_keys = set()
    # for item in result[0]["模板要写入的值"]:
    #     all_keys.update(item.keys())
    # print(all_keys)
    # print(str(all_keys))
